<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>管理员列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../layui/css/layui.css" tppabs="static/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="demoTable">
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <div class="layui-inline">
        <input class="layui-input" name="aname" id="aname" placeholder="管理员姓名" autocomplete="off">
    </div>
    <div class="layui-inline">
        <input class="layui-input" name="aphone" id="aphone" placeholder="管理员手机号" autocomplete="off">
    </div>
    <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
</div>

<table class="layui-hide" id="test" lay-filter="test"></table>
<!--自定义的控件-->
<script type="text/html" id="toolbarDemo" style="display: none; ">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="insert">添加管理员</button>
        <button class="layui-btn layui-btn-sm" lay-event="flash">刷新</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="det">详情</a>
    <a class="layui-btn layui-btn-xs" lay-event="upd">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="/static/js/jquery-3.3.1.js"></script>
<script src="../../layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->
<script>
    layui.use(['table','layer','jquery'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var $=layui.$;

        table.render({
            elem: '#test'
            ,url:'/adminquery'
            ,page: true
            ,id:'testReload'
            ,cols: [[
                {field:'aid', title:'ID', width:60,  unresize: true, sort: true}
                ,{field:'auserId',title:'用户编号', width:150}
                ,{field:'aposition', title:'角色', width:150,
                    templet: function (da){
                        if(da.aposition == 0){
                            return "超级管理员";
                        }else if(da.aposition == 1){
                            return "管理员";
                        }else if(da.aposition == 2){
                            return "员工";
                        }
                    }
                } //edit: 'text'   开启页面修改
                ,{field:'auser', title:'账号', width:100}
                ,{field:'apassword', title:'密码', width:100}
                ,{field:'aname', title:'姓名', width:100}
                ,{field:'asex', title:'性别', width:100}
                ,{field:'aphone', title:'手机号', width:150}
                ,{field:'atime', title:'注册时间', width:140,
                    templet: "<div>{{layui.util.toDateString(d.atime,'yyyy-MM-dd')}}</div>"
                }
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:200}
            ]],
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            parseData: function (rs) {//数据格式解析
                if (rs.code == 0) {
                    console.log(document.body.clientHeight);
                    return {
                        "code": rs.code,
                        "msg": rs.msg,
                        "count": rs.data.total,
                        "data": rs.data.list
                    }
                }
            }
            ,height: document.documentElement.clientHeight - 130
        });


        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'insert':
                    layer.open({
                        type: 2
                        ,title: '添加界面'
                        ,content: "/static/view/admin/insert.html"
                        ,area: ["400px", '550px']
                        ,fixed: false
                        ,shadeClose: true,
                        cancel: function () {
                        }, success: function (layero, index) {
                        }, end: function () {
                            table.reload('testReload');
                        }
                    });
                    break;
                case 'flash':
                    table.reload('testReload');
                    break;
            };

        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'det'){
                open("详情页面",'/static/view/admin/details.html?aid='+data.aid);
            } else if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        type: "POST",
                        url: "/adminupdate2",
                        data: {"aid": data.aid},
                        async:true,
                        dataType: 'json',
                        success: function (da) {
                            if (da.code === 0) {
                                layer.msg("删除成功！")
                                obj.del(); //删除对应行（tr）的DOM结构
                                // location.reload();//刷新当前页面
                            } else {
                                layer.msg(da.msg)
                            }
                        },
                        error: function (da) {
                            alert("出现错误");
                            return false;
                        }
                    })
                    layer.close(index);
                });
            } else if(obj.event === 'upd'){
                open("修改页面","/static/view/admin/update.html?aid="+data.aid);
            }
        });

        //打开弹窗
        function open (title,url) {
            layer.open({
                type: 2
                ,title: title
                ,content: url
                ,area: ["400px", "550px"]
                ,fixed: false
                ,shadeClose: true,
                cancel: function () {
                }, success: function (layero, index) {
                }, end: function () {
                    table.reload('testReload');
                }
            });
        }

        //条件查询
        var $ = layui.$, active = {
            reload: function () {
                var aname = $('#aname');
                var aphone = $('#aphone');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        "aname": aname.val(),
                        "aphone":aphone.val()
                    }
                });
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>

</body>
</html>